<?php

namespace app\admin\model;

use think\Model;


class RechargeRule extends Model
{

    

    

    // 表名
    protected $name = 'recharge_rule';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    







    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function GetPriceRechargeGiveAmount($amount, $category_id = 0)
    {
        //如果category_id不为0,则先查找category_id对应的充值规则
        if ($category_id) {
            $rechargeRule = RechargeRule::where('amount', $amount)->where('category_id', $category_id)->find();
            if ($rechargeRule) {
                return $rechargeRule->give_amount;
            }
        }
        //如果category_id为0,或者category_id对应的充值规则不存在,则查找category_id为0的充值规则
        $rechargeRule = RechargeRule::where('amount', $amount)->where('category_id', 0)->find();
        if ($rechargeRule) {
            return $rechargeRule->give_amount;
        }
        return 0;
    }


}
